package geekbang.single;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xuyanqing
 * @description ✨枚举类实现单例，通过 Java 枚举类型本身的特性，保证了实例创建的线程安全性和实例的唯一性
 * @date 2025-10-11 15:58
 * @creed where there is a will,there is a way. yx
 */
public enum IdGeneratorEnum {
    INSTANCE;
    private AtomicInteger id = new AtomicInteger(0);

    public long getId() {
        return id.incrementAndGet();
    }
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(IdGeneratorEnum.INSTANCE.getId())).start();
        }
    }
}
